#include "dri_io.h"
#include "doble_list.h"
#include "touch.h"
//int stage = 1, d = 0;

SL bmp_head;
SL avi_head;
SL mp3_head;

int i=0;//显示图片位置全局


void double_list_init()
{
    myslInit(&bmp_head);
    myslInit(&avi_head);
    myslInit(&mp3_head);
}



//函数功能：目录检索
//参数说明：path目录，c文件类型，type链表类型：1 bmp,2 avi,3 mp3
//返回值  ：无
void dri_io_init(char *path, char *c,int type)
{
    
    


    char buf_path[1024] = "";
    struct dirent *Dir_Info = NULL;

    // printf("input dri:");
    // char path[] = "";
    // scanf("%s", path);

    DIR *dir2 = opendir(path);
    // DIR *dir2 = opendir("../1021");
    if (dir2 == NULL)
    {
        printf("opendir2 error..\n");
    }
  
  
    while (1)
    {
        Dir_Info = readdir(dir2);
        if (Dir_Info == NULL)
        {
            rewinddir(dir2);
            // continue;
            break;
        }

        if (Dir_Info->d_name[0] == '.')
        {
            continue;
        }

        if (strstr(Dir_Info->d_name, c))
        {
            /* code */
            // printf("Dir_Info->d_name:%s\n",Dir_Info->d_name);
            sprintf(buf_path, "%s/%s", path, Dir_Info->d_name);
            
            printf("%s\n", buf_path);
            if (type==1)
            {
                insert(&bmp_head, buf_path);
            }
             if (type==2)
            {
                insert(&avi_head, buf_path);
            }
             if (type==3)
            {
                insert(&mp3_head, buf_path);
            }
            
            
            Display_characterX( 0,                // x坐标起始点
                                i*30,                // y坐标起始点
                                buf_path,         // GB2312 中文字符串
                                0xFF0000,         //字体颜色值
                                2);
            i++;

        }
    }
    i=0;
    printf("========================================================\n");
    // while (1)
    // {
    //     // touching();
    //     // if (t == 0)
    //     // {
    //     //     printf("%d  %d  %d\n", x, y, t);
    //     // }
    //     printf("up 1  down 2");
    //     scanf("%d", &d);
    //     switch (d)
    //     {
    //     case 1:
    //         stage = cheak_up(&head, stage);
    //         break;
    //     case 2:
    //         stage = cheak_down(&head, stage);
    //         break;

    //     default:
    //         break;
    //     }
    // }

    closedir(dir2);
}

int file_find(char *path,SL * list_head,char *c)
{
	struct dirent *dir_entry = NULL;
	// char Ob_path[512];
	char path_New[1024];

	// bzero(Ob_path,512);
	bzero(path_New, 1024);

	DIR *dir = opendir(path);
	if (dir == NULL)
	{
		perror("opendir...");
		return -1;
	}

	while (1)
	{
		dir_entry = readdir(dir);
		if (dir_entry == NULL)
			break;

		if (*(dir_entry->d_name) == '.')
			continue;

		if (dir_entry->d_type == DT_DIR)
		{
			bzero(path_New, 1024);
			sprintf(path_New, "%s/%s", path, dir_entry->d_name);
			//printf("***%s***\n", path_New);
			if (strstr(dir_entry->d_name, c))
			{
				printf("%s\n", path_New);
				insert(list_head,path_New);
			}
			file_find(path_New,list_head,c);
		}
		else
		{
			bzero(path_New, 1024);
			sprintf(path_New, "%s/%s", path, dir_entry->d_name);
			//printf("%s\n", path_New);
			if (strstr(dir_entry->d_name, c))
			{
				printf("%s\n", path_New);
				insert(list_head,path_New);
			}
		}
	}

	// bzero(Ob_path,512);
	bzero(path_New, 1024);

	closedir(dir);

	//return 0;
}